﻿using DataAccess;
using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace QuanLyKhachSan
{
    public partial class FormTimNhanVien : XtraForm
    {
        public FormTimNhanVien()
        {
            InitializeComponent();
            Result = new List<NhanVien>();
        }

        public object Giatri { get; set; }
        private TimNhanVienTheo _kieutim;
        public List<NhanVien> Result;
        private readonly NhanVienManager _manager = new NhanVienManager();
        
        private void cmbTieuchiTK_SelectedIndexChanged(object sender, EventArgs e)
        {
            var index = cmbTieuchiTK.SelectedIndex;
            switch (index)
            {
                case 0: case 1: case 2:
                    txtGiatri.Enabled = true;
                    cmbChucvu.Enabled = false;
                    radNam.Enabled = radNu.Enabled = false;
                    dtpNgaylamviec.Enabled = false;
                    _kieutim = index == 0 ? TimNhanVienTheo.TheoMa : index == 1 ? TimNhanVienTheo.TheoTen : TimNhanVienTheo.TheoSoCMND;
                    break;
                case 3:
                    txtGiatri.Enabled = false;
                    cmbChucvu.Enabled = true;
                    radNam.Enabled = radNu.Enabled = false;
                    dtpNgaylamviec.Enabled = false;
                    _kieutim = TimNhanVienTheo.TheoChucVu;
                    break;
                case 4:
                    txtGiatri.Enabled = false;
                    cmbChucvu.Enabled = false;
                    radNam.Enabled = radNu.Enabled = true;
                    dtpNgaylamviec.Enabled = false;
                    _kieutim = TimNhanVienTheo.TheoGioiTinh;
                    break;
                case 5:
                    txtGiatri.Enabled = false;
                    cmbChucvu.Enabled = false;
                    radNam.Enabled = radNu.Enabled = false;
                    dtpNgaylamviec.Enabled = true;
                    _kieutim = TimNhanVienTheo.TheoNgayLamViec;
                    break;
                default:
                    break;
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void LoadChucVu()
        {
            cmbChucvu.DataSource = new ChucVuManager().LayDanhSach().Result;
            cmbChucvu.DisplayMember = "TenCV";
            cmbChucvu.ValueMember = "MaCV";
        }

        private void btnTimkiem_Click(object sender, EventArgs e)
        {
            Func<NhanVien,bool> func=nv => nv.MaNV.Equals(1);
            Giatri = GetGiaTri();
            switch (_kieutim)
	        {
		        case TimNhanVienTheo.TheoMa:
                 func = nv => nv.MaNV.Equals(int.Parse(Giatri.ToString()));
                 break;
                case TimNhanVienTheo.TheoTen:
                 func = nv => nv.TenNhanVien.Contains(Giatri.ToString());
                 break;
                case TimNhanVienTheo.TheoSoCMND:
                 func = nv => nv.SCMND.Contains(Giatri.ToString());
                 break;
                case TimNhanVienTheo.TheoChucVu:
                 func = nv => nv.MaCV.Contains(Giatri.ToString());
                 break;
                case TimNhanVienTheo.TheoGioiTinh:
                 func = nv => nv.GioiTinh.Equals((bool)Giatri);
                 break;
                case TimNhanVienTheo.TheoNgayLamViec:
                 func = nv => nv.NgayLamViec.Equals(DateTime.Parse(Giatri.ToString()));
                 break;
                default:
                 break;
	        }

            var inum = _manager.LayDanhSach().Result.Where(func);
            if (!inum.Any())
                XtraMessageBox.Show("Không tìm thấy nhân viên", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
            {
                Result = inum.ToList();
                Close();
            }
        }
        
        private object GetGiaTri()
        {
            if (txtGiatri.Enabled)
                return txtGiatri.Text;
            else
                if (cmbChucvu.Enabled)
                    return cmbChucvu.SelectedValue;
                else
                    if (radNam.Enabled)
                        return radNam.Checked ? true : false;
                    else
                        return dtpNgaylamviec.Value.ToShortDateString();
        }

        private void FormTimNhanVien_Load(object sender, EventArgs e)
        {
            LoadChucVu();
        }
    }
}
